package com.fancy.learncenter.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.view.PromptPopupWindow;
import com.superservice.lya.R;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class YgtMouldActivity extends BaseActivity {


    @Bind(R.id.mould_code)
    EditText mouldCode;
    @Bind(R.id.rootView)
    LinearLayout rootView;
    @Bind(R.id.temp)
    TextView temp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mould_ygt, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);

        isSys = getIntent().getBooleanExtra("isSys", false);

        if (isSys) {
            initToolbar("同步系统模板");

            temp.setText("同步系统模板");

            mouldCode.setVisibility(View.GONE);
        } else {
            initToolbar("同步他人模板");
        }

    }

    boolean isSys = false;

    private void copyYgt() {
        Map map = new HashMap();
        map.put("code", mouldCode.getText().toString());
        HttpMehtod.getInstance().syncYgt(map, new IdeaObserver<BaseDataBean>(this,true) {

            @Override
            public void onSuccess(BaseDataBean ygtDataBeanBaseDataBean) {
                ToastUtil.show("同步完成");
//                Intent intent = new Intent(YgtMouldActivity.this, YgtDetailActivity.class);
////                intent.putExtra("classifyId", adapter.freeId);
//                startActivity(intent);
            }

            @Override
            public void onFail(int errorCode) {
            }
        });
    }

    private void syncSys() {
        Map map = new HashMap();
        HttpMehtod.getInstance().syncSys(map, new IdeaObserver<BaseDataBean>(this,true) {

            @Override
            public void onSuccess(BaseDataBean ygtDataBeanBaseDataBean) {
                ToastUtil.show("同步完成");
//                Intent intent = new Intent(YgtMouldActivity.this, YgtDetailActivity.class);
////                intent.putExtra("classifyId", adapter.freeId);
//                startActivity(intent);
            }

            @Override
            public void onFail(int errorCode) {
            }
        });
    }

    PromptPopupWindow promptPopupWindow;

    @OnClick(R.id.submit)
    public void onViewClicked() {

        if (isSys) {


            if (promptPopupWindow == null) {
                promptPopupWindow = new PromptPopupWindow(this, rootView);
                promptPopupWindow.setPromptTitle("复制系统模板 \n 本地项目将清除");
                promptPopupWindow.setCallBack(new PromptPopupWindow.CallBack() {
                    @Override
                    public void confirm() {
                        syncSys();
                    }

                    @Override
                    public void cancle() {

                    }
                });
            }
            promptPopupWindow.showPopupWindow();
        } else if (!TextUtils.isEmpty(mouldCode.getText().toString()) && mouldCode.getText().toString().length() == 6) {
            if (promptPopupWindow == null) {
                promptPopupWindow = new PromptPopupWindow(this, rootView);
                promptPopupWindow.setPromptTitle("复制对方模板 \n 本地项目将清除");
                promptPopupWindow.setCallBack(new PromptPopupWindow.CallBack() {
                    @Override
                    public void confirm() {
                        copyYgt();
                    }

                    @Override
                    public void cancle() {

                    }
                });
            }
            promptPopupWindow.showPopupWindow();
        } else {
            ToastUtil.show("请输入正确的模板码");
        }

    }
}
